<?php
/**
 * The wp-admin console screens for Achievements. This file contains some logic to handle the different admin
 * screens, and implementation of the non-custom post type customisations.
 *
 * @author Paul Gibbs <paul@byotos.com>
 * @package Achievements
 * @subpackage admin
 * @see dpa_add_admin_menu()
 *
 * $Id: general.php 981 2011-05-02 21:09:24Z DJPaul $
 */

/**
 * Detect when our custom post type screen has been loaded and trigger an appropriate action to easily load all of our custom stuff.
 *
 * This won't load for each admin screen; check dpa_setup_admin_menu()
 *
 * @see dpa_setup_admin_menu()
 * @since 2.1
 * @todo Find way of detecting post type on wp-admin/post.php?post=x&action=edit without get_post_type().
 */
function dpa_admin_load_admin_menu() {
	$screen = get_current_screen();
	if ( empty( $screen ) )
		return;

	// wp-admin/post-new.php?post_type=dpa_achievements
	if ( 'post' == $screen->base && 'dpa_achievements' == $screen->post_type ) {
		require_once ( dirname( __FILE__ ) . '/achievements.php' );
		do_action( 'dpa_admin_load_admin_menu_achievements' );

	// wp-admin/post.php?post=x&action=edit
	} elseif ( 'post' == $screen->base && 'post' == $screen->post_type ) {
		if ( empty( $_REQUEST['post'] ) )
			return;

		$post_id   = (int) $_REQUEST['post'];
		$post_type = get_post_type( $post_id );

		if ( !empty( $post_type ) && 'dpa_achievements' == $post_type ) {
			require_once ( dirname( __FILE__ ) . '/achievements.php' );
			do_action( 'dpa_admin_load_admin_menu_achievements' );
		}
	}
}

/**
 * Callback for register_post_type to customise the meta boxes on the achievement post type edit's page.
 *
 * This can't go in /admin/achievements.php due to load order dependencies with registering the callback on register_post_type().
 *
 * @param unknown $post Post object
 * @since 2.1
 */
function dpa_admin_achievements_cb( $post ) {
	// Hide dpa_action taxonomy box
	remove_meta_box( 'dpa_actiondiv', 'dpa_achievements', 'side' );

	// Show Achievements' general metabox
	add_meta_box( 'dpa_achievements_mb', __( 'Achievement Settings', 'dpa' ), 'dpa_admin_achievements_mb', 'dpa_achievements', 'side' );

	// Show Achievements' action metabox
	add_meta_box( 'dpa_achievements_event_mb', __( 'Achievement Events', 'dpa' ), 'dpa_achievements_event_mb', 'dpa_achievements', 'normal' );
}
?>